package com.itheima.reggin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggin.common.BaseContext;
import com.itheima.reggin.common.R;
import com.itheima.reggin.entity.Orders;
import com.itheima.reggin.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }


    /**
     * 展示订单数据信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize) {
        Long id = BaseContext.getId();
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Orders::getUserId, id);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }


    @GetMapping("/page")
    public R<Page> pageBack(int page, int pageSize, String number, String beginTime, String endTime) {
        log.info("进入查询方法" + page + "-" + pageSize + "-" + number + "-" + beginTime + "-" + endTime);
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(!StrUtil.isEmpty(number), Orders::getNumber, number);
        queryWrapper.ge(!StrUtil.isEmpty(beginTime), Orders::getOrderTime, beginTime);
        queryWrapper.le(!StrUtil.isEmpty(endTime), Orders::getOrderTime, endTime);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    //派送
    @PutMapping()
    public R send(@RequestBody Orders orders) {
        Orders byId = orderService.getById(orders.getId());
        byId.setStatus(orders.getStatus());
        boolean b = orderService.updateById(byId);
        return R.success("修改成功！！！");
    }

}

